package com.icas.controller;

import com.alibaba.fastjson.JSONObject;
import com.icas.entity.databaseentity.AccountEntity;
import com.icas.entity.databaseentity.AllCourseEntity;
import com.icas.entity.databaseentity.StuAttend;
import com.icas.entity.resultentity.AttendCountEntity;
import com.icas.entity.resultentity.Result;
import com.icas.entity.resultentity.StuCourseHistoryAttendEntity;
import com.icas.entity.resultentity.StuListResultEntity;
import com.icas.enums.MsgResultEnum;
import com.icas.exception.QueryNotFoundException;
import com.icas.service.AccountService;
import com.icas.service.AttendService;
import com.icas.service.CountService;
import com.icas.service.CourseService;
import com.icas.util.StringUtils;
import com.icas.util.jwt.JwtUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: tocchen
 * @Date: 2022/4/18 23:02
 * @Description 登录完成后一些功能的实现
 **/
@RestController
@RequestMapping("/kingzhihui/my/search")
@RequiresAuthentication
public class SearchController {

    @Autowired AccountService accountService;

    @Autowired CourseService courseService;

    @Autowired AttendService attendService;

    @Autowired CountService countService;

    /**
     * 所有角色，只要过Filter就能操作
     * 解析token，获取账号，查询账号对应的课表信息
     * @param token token值
     * @return 结果集
     */
    @PostMapping("/course_list")
    @RequiresRoles(logical = Logical.OR,value = {"0","1","2"})
    public Result searchCourse(@RequestHeader(value = "token") String token){
        String id = JwtUtils.getId(token);

        if ("".equals(id) || ObjectUtils.isEmpty(id)){
            return Result.error(MsgResultEnum.QUERY_FAILED);
        }
        AccountEntity accountEntity = accountService.byIdGetInfo(id);
        if (ObjectUtils.isEmpty(accountEntity)){
            return Result.error(MsgResultEnum.QUERY_FAILED);
        }
        return Result.oK(accountEntity.getCourses());
    }

    /**
     * 获取对应课程的学生列表
     * @param json 课程id
     * @return 结果集
     */
    @PostMapping("/stu_list")
    @RequiresRoles(logical = Logical.OR, value = {"0","1"})
    public Result searchStudents(@RequestBody JSONObject json) {

        String id = json.getString("courseId");

        AllCourseEntity allCourseEntity = courseService.byIdGetInfo(id);

        if (ObjectUtils.isEmpty(allCourseEntity)){
            return Result.error(MsgResultEnum.QUERY_FAILED);
        }else{
            return Result.oK(new StuListResultEntity(id,allCourseEntity.getStuList()));
        }
    }

    /**
     * 获取历史签到表，按最新时间排序
     * @param json json json格式 {"courseId":"value"}
     * @return 结果集
     */
    @PostMapping("/attend_list")
    @RequiresRoles(logical = Logical.OR, value = {"0","1"})
    public Result searchAttendList(@RequestBody String json) {

        JSONObject jsonObject = JSONObject.parseObject(json);
        String courseId = jsonObject.getString("courseId");
        return attendService.byCourseIdGetAttendList(courseId);

    }

    /**
     * 获取具体签到表的所有学生信息
     * @param json json
     * @return
     */
    @PostMapping("/attend_list/attend")
    @RequiresRoles(logical = Logical.OR, value = {"0","1"})
    public Result searchAttend(@RequestBody JSONObject json){

        String sheetId = json.getString("sheetId");
        if (StringUtils.isEmpty(sheetId)){
            throw new QueryNotFoundException();
        }
        List<StuAttend> stuAttends = attendService.byAttendSheetIdGetStuList(sheetId);
        // TODO 当前查询耗时过长，减少后端处理时间，不进行分组返回
        return ObjectUtils.isEmpty(stuAttends)?Result.error(MsgResultEnum.QUERY_FAILED):Result.oK(stuAttends);

    }

    /**
     * TODO 待优化的controller
     * 查询某个学生的签到统计
     * @param json 学生账号和课程id
     * @return
     */
    @PostMapping("/attend/stu")
    @RequiresRoles(logical = Logical.OR, value = {"0","1"})
    public Result searchAttendStuList(@RequestBody JSONObject json){

        String stuId = json.getString("stuId");
        String courseId = json.getString("courseId");

        if (StringUtils.isEmpty(stuId,courseId)){
            throw new QueryNotFoundException();
        }
        AttendCountEntity attendCount = CountService.defaultUserServer.getAttendCount(courseId,stuId);
        return ObjectUtils.isEmpty(attendCount)?Result.error(MsgResultEnum.QUERY_FAILED):Result.oK(attendCount);
    }

    /**
     * 学生查询某个课程的签到统计
     * @param json 携带课程id的token值
     * @return
     */
    @PostMapping("/course/attend")
    @RequiresRoles(logical = Logical.OR, value = {"0","2"})
    public Result searchStuAttendCount(@RequestBody JSONObject json,@RequestHeader("token") String token){

        String courseId = json.getString("courseId");
        String account = JwtUtils.getAccount(token);

        if (StringUtils.isEmpty(courseId,account)){
            throw new QueryNotFoundException();
        }

        AttendCountEntity attendCount = CountService.defaultUserServer.getAttendCount(courseId,account);
        return ObjectUtils.isEmpty(attendCount)?Result.error(MsgResultEnum.QUERY_FAILED):Result.oK(attendCount);
    }


    /**
     * 查询课程的签到统计
     * @param json 携带课程Id的json值
     * @return
     */
    @PostMapping(value = "/course_count")
    @RequiresRoles(logical = Logical.OR, value = {"0","1"})
    public Result searchCourseAttend(@RequestBody JSONObject json){
        String courseId = json.getString("courseId");

        if (StringUtils.isEmpty(courseId)){
            throw new QueryNotFoundException();
        }

        AttendCountEntity allStuHistoryAttend = countService.getAllStuHistoryAttend(courseId);
        return ObjectUtils.isEmpty(allStuHistoryAttend)?Result.error(MsgResultEnum.QUERY_FAILED):Result.oK(allStuHistoryAttend);
    }

    /**
     * 学生在某个课程的签到历史信息
     * @date 2022-4-24 21:43
     * @param json json值，需传入课程id
     * @param token token值
     * @return Result
     */
    @PostMapping(value = "/course_stu/attend")
    @RequiresRoles(logical = Logical.OR, value = {"0","2"})
    public Result searchCourseAttendStu(@RequestBody String json,@RequestHeader("token") String token){
        JSONObject jsonObject = JSONObject.parseObject(json);
        String courseId = jsonObject.getString("courseId");
        String account = JwtUtils.getAccount(token);
        List<StuCourseHistoryAttendEntity> stuHistoryAttend = attendService.getStuHistoryAttend(courseId, account);

        return ObjectUtils.isEmpty(stuHistoryAttend)?Result.error(MsgResultEnum.ERROR):Result.oK(stuHistoryAttend);
    }



}
